package model.camera;

import java.util.Date;

import model.ThreadHandler;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.text.format.Time;
import android.util.Log;

public class CameraPreviewCallback implements Camera.PreviewCallback 
{
	private static final String TAG = CameraPreviewCallback.class.getSimpleName();
		
	private ThreadHandler _handler;
	private long lastTime;
	private Time time;
	
	public CameraPreviewCallback(ThreadHandler handler) 
	{
		_handler = handler;
		time = new Time();
		time.setToNow();
		lastTime = time.toMillis(true);
	}

	/**
	 * Gets called continuously when camera.setPreviewCallback is called.
	 * <p>Starts decoding current frame</p>
	 */
	@Override
	public void onPreviewFrame(byte[] bytes, Camera camera) 
	{
		time.setToNow();
		Log.d("TIME", String.format("OnPreviewFrame() : %d", (time.toMillis(true)-lastTime)));
		lastTime = time.toMillis(true);
		
		Size previewSize = camera.getParameters().getPreviewSize();
		Log.d(TAG, String.format("Amount of Threads: %d", Thread.activeCount()));
		
		new Decoder(previewSize, _handler).execute(bytes);
	}
}
